﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Manager_personnel
{
    class clsdulieu : clsconnect
    {
        #region contructer
        public clsdulieu()
            : base()
        {


        }
        #endregion


        #region đăng nhập

        public int login(string username, string pass)
        {
                ketnoiCSDL();

                string query = @"select Count(username) from [user] where username='admin' and password='admin'";
                SqlCommand com = new SqlCommand(query, con);
              
                int count = Convert.ToInt32(com.ExecuteScalar());
                return count;
       

           
           
            
        }


        #endregion




        #region kiểm tra trùng khóa

        public bool chekkey(string username, string email)
        {
            ketnoiCSDL();
            string query = @"select * from [dbo].[user] where username ='" + username + "' or email = '" + email + "'";
            SqlCommand com = new SqlCommand(query, con);
            int count = Convert.ToInt16(com.ExecuteNonQuery());
            if (count > 0)
            {
                // trùng key
                return false;
            }
            else
            {
                //ko bị trùng key 
                return true;
            }
        }
        #endregion


        #region đăng kí
        public bool sign_in(string username, string pass, string email, string name)
        {
            ketnoiCSDL();
            try
            {

                if (chekkey(username, email) == true)
                {
                    //tiếp tục insert 

                    string query = @"insert into  [dbo].[user] values ('" + username + "','" + pass + "','" + name + "','" + email + "')";
                    SqlCommand com = new SqlCommand(query, con);
                    int count = Convert.ToInt16(com.ExecuteNonQuery());
                    if (count > 0)
                    {
                        //thành công
                        return true;
                    }
                }
                else
                {
                    // key đã bị trùng thông báo đắng kí thất bại 
                    return false;
                }
            }
            catch (Exception error)
            {

            }
            finally
            {

                dongketnoi();
            }

            return false;

        }

        #endregion


        #region thêm
        public void them(string table)
        {

        }
        #endregion


        #region xóa
        public void xoa(string table)
        { }
        #endregion

        #region sửa
        public void sua(string table)
        { }
        #endregion



        #region datatable gridview 
        public DataTable GetData(string sql)
        {
            DataTable db = new DataTable();
            try
            {

                ketnoiCSDL();
                SqlDataAdapter dapter = new SqlDataAdapter(sql, con);
                dapter.Fill(db);
            }
            catch (Exception error)
            { }
            finally {
                dongketnoi();
            }
            return db;
        }

        #endregion


    }
}
